import { getNewsDetail, labelQueryList, parentCode, submit, update, deleteDetail, getEstateList } from './infoDetailServ'
// 弹出消息框
import { message } from 'antd'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import { makeAddress } from "utils/util"
import { messageInform } from '../../../../utils/notification';

// 初始默认状态
const state = {
    btnLoading: false,
    btndisabled: false,
    skipType: '',
    fengmianList: [],
    editValue: 1,
    showEditor: true,
    disabledLink: true,
    productId: '',
    activityDetail: [],
    detail: {},
    tagALL: [],
    reaList: [],
    areaList: [],
    showTime: true,
    visible: false,
    imgModal: {
        visible: false,
        srcs: [],
    },
    isSubmit: true,
    isPush: false
}

//获取ant Images
function getImagesUrl(ImageList) {
    console.log(ImageList)
        //返回一个数组
    let arr = []
    ImageList && ImageList.map((item, idx) => {
        console.log(item.response.data.url)
        arr.push(item.response.data.url)
    })
    return arr
}
//生成ant Images 格式
function setImagesUrl(url) {
    //返回一个数组
    let arr = []
    arr.push({
        uid: -1,
        name: 'xxx.png',
        status: 'done',
        url: url
    })
    return arr
}

const tmpModule = {
    // namespace: 'infoAddOrEditModel',
    state: cloneDeep(state),
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                let { actionType, contentId } = query
                if ('' + tmpModule.pathname === '' + pathname) {
                    //每次进来页面更新数据 重置state
                    console.log('subscriptions')
                    dispatch({ type: 'updateStore', payload: cloneDeep(state) });
                    dispatch({ type: 'updateStore', payload: { actionType, contentId } });
                    dispatch({ type: 'getLabelQueryList', payload: {} });
                    dispatch({ type: 'getAreaList', payload: {} });
                    if (actionType == 'edit') {
                        dispatch({ type: 'estateInfo', payload: { actionType, contentId } });
                    } else if (actionType == 'detail') {
                        dispatch({ type: 'estateInfo', payload: { actionType, contentId } });
                    }
                }
            })
        },
    },
    effects: {
        // 查询记录
        * estateInfo({ payload }, { put, call, select }) {
            let { contentId } = payload
            try {
                const { data, resultCode, resultMsg } = yield call(getNewsDetail, { contentId })
                if (data.tags) {
                    let tagsArr = data.tags.split(',')
                    data.tags = tagsArr
                }
                // let showTime = true
                // if(data.channel == 3){
                //   showTime = false
                // }
                let editValue = 1,
                    showEditor = 'block',
                    disabledLink = true
                if (data.linkFlag) {
                    editValue = 2
                    showEditor = 'none'
                    disabledLink = false
                }
                console.log(111, data);
                yield put({ type: 'updateStore', payload: { detail: data, editValue, showEditor, disabledLink } })

            } catch (e) {

            }
        },
        //获取标签列表
        * getLabelQueryList({ payload }, { put, call, select }) {
            try {
                const { rows } = yield call(labelQueryList, { type: 3, pageNo: 1, pageSize: 1000 })
                yield put({ type: 'updateStore', payload: { tagALL: rows } });
            } catch (e) {

            }
        },
        //获取级联列表
        * getAreaList({ payload }, { put, call, select }) {
            try {
                const { data, success } = yield call(parentCode);
                if (success) {
                    let areaList = []
                    let arr = [data[0]]
                    let attr = [data[data.length - 1]]
                    data[0] = {
                        label: "北京",
                        value: "110100",
                        children: arr
                    }
                    data[data.length - 1] = {
                            label: "澳门",
                            value: "820000",
                            children: attr
                        }
                        //去掉第三级，
                    areaList = makeAddress(data, 2)
                    yield put({ type: 'updateStore', payload: { areaList } });
                }
            } catch (e) {

            }
        },
        //获取项目列表
        * getBusinessList({ payload }, { put, call, select }) {
            let params = {
                pageNo: 1,
                pageSize: 1000,
                ...payload
            }
            try {
                const data = yield call(getEstateList, params)
                let estateList = []
                if (data.success) {
                    estateList = data.rows
                }
                yield put({ type: 'updateStore', payload: { estateList } });
            } catch (e) {

            }
        },
        * deleteContent({ payload }, { put, call, select }) {
            try {
                let { contentId } = yield select(state => state[tmpModule.namespace])
                    // contentId = Number(contentId)
                console.log(contentId)
                yield put({ type: 'updateStore', payload: { loading: true, btnLoading: true, } })
                const data = yield call(deleteDetail, { 'contentId': contentId })
                yield put({ type: 'updateStore', payload: { loading: false, btnLoading: false, } })
                if (data.success) {
                    message.success('删除成功').then(() => {
                        hashHistory.push({ pathname: MODULE_TO_ROUTES[tmpModule.namespace].urlArray[1] })
                    });
                }
            } catch (e) {

            }
        },
        * sumbit({ payload }, { put, call, select }) {
            try {
                let tmpState = yield select(state => state[tmpModule.namespace])
                let { detail } = tmpState
                console.log('details', detail)
                if (typeof(detail.titleImgUrl) == "undefined" || detail.titleImgUrl == '') {
                    message.error('请先上传封面图')
                    return false
                }
                detail.channel = 1
                    //处理提交数据
                if (detail.tags && detail.tags.length != 0) {
                    let tagsStr = ''
                    let arr = detail.tags.join(',')
                    detail.tags = arr
                } else {
                    detail.tags = ''
                }
                // 编辑或新增
                //console.log(detail)

                yield put({ type: 'updateStore', payload: { loading: true, btnLoading: true, } })
                if (tmpState.actionType == 'add') {

                    const data = yield call(submit, detail)
                    yield put({ type: 'updateStore', payload: { loading: false, btnLoading: false, btndisabled: true } })
                    if (data.success) {
                        yield put({ type: 'updateStore', payload: { estateList: [] } });
                        message.success('提交成功').then(() => {
                            hashHistory.push({ pathname: MODULE_TO_ROUTES[tmpModule.namespace].urlArray[1] })
                        });
                    } else {
                        yield put({ type: 'updateStore', payload: { btndisabled: false } })
                    }
                } else {
                    detail.contentId = tmpState.contentId
                    const data = yield call(update, detail)
                    yield put({ type: 'updateStore', payload: { loading: false, btnLoading: false, btndisabled: true } })
                    if (data.success) {
                        message.success('编辑成功').then(() => {
                            hashHistory.push({ pathname: MODULE_TO_ROUTES[tmpModule.namespace].urlArray[1] })
                        });
                    } else {
                        yield put({ type: 'updateStore', payload: { btndisabled: false } })
                    }
                }
            } catch (e) {
                console.log(e)
            }
        },
        * updateModel({ payload }, { put, select }) {
            console.log('updateModel')
            yield put({ type: 'updateStore', payload });
        },
        * dealEditor({ payload }, { put, select }) {
            let { detail } = yield select(state => state[tmpModule.namespace])
            let newDetail = Object.assign({}, detail)
            newDetail.detail = payload.detail
            yield put({ type: 'updateStore', payload: { detail: newDetail } });
        },
    },

    // 存入状态机
    reducers: {
        updateStore(preState, action) {
            console.log(preState, action)
            return {...preState, ...action.payload };
        }
    }
}

export default tmpModule